package com.bulletelc.apollopre.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bulletelc.apollopre.R;

/**
 * Created by CX on 2018/4/13.
 */

public class FoolSortNav extends LinearLayout implements View.OnClickListener {

    private LinearLayout llRecomment, llPrice, llSale, llHot;
    private ImageView ivPriceSort;

    // 价格排序，10升序 / 20降序 / -1默认不排
    private int priceSort = 20;
    private boolean priceShow = false;

    private LayoutInflater inflater;

    private onNavClickCallback navClickCallback;

    public FoolSortNav(Context context) {
        super(context);
    }

    public FoolSortNav(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.fool_sortnav_layout,this, true);
        inflater = LayoutInflater.from(context);

        llPrice = view.findViewById(R.id.llPrice);
        llRecomment = view.findViewById(R.id.llRecomment);
        llSale = view.findViewById(R.id.llSale);
        llHot = view.findViewById(R.id.llHot);

        ivPriceSort = view.findViewById(R.id.ivPriceSort);

        llPrice.setOnClickListener(this);
        llRecomment.setOnClickListener(this);
        llSale.setOnClickListener(this);
        llHot.setOnClickListener(this);

        showBorderLine(llPrice);

    }

    public FoolSortNav(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llRecomment:
                showBorderLine(llRecomment);
                if (navClickCallback != null) {
                    navClickCallback.onClickRecommendSort();
                    priceShow = true;
                }
                break;
            case R.id.llPrice:

                showBorderLine(llPrice);
                if (navClickCallback != null) {
                    if (priceShow) {
                        priceShow = false;
                        navClickCallback.showFragment();
                    } else {
                        switch (priceSort) {
                            case 10:
                                navClickCallback.onClickPriceSortAsc();
                                ivPriceSort.setImageResource(R.mipmap.ic_asc);
                                priceSort = 20;
                                break;
                            case 20:
                                navClickCallback.onClickPriceSortDesc();
                                ivPriceSort.setImageResource(R.mipmap.ic_desc);
                                priceSort = 10;
                                break;
                        }
                    }
                }
                break;
            case R.id.llSale:
                showBorderLine(llSale);
                if (navClickCallback != null) {
                    navClickCallback.onClickSaleSort();
                    priceShow = true;
                }
                break;
            case R.id.llHot:
                showBorderLine(llHot);
                if (navClickCallback != null) {
                    navClickCallback.onClickHotSort();
                    priceShow = true;
                }
                break;
        }
    }

    private void showBorderLine(ViewGroup res) {
        View borderLine = findViewById(R.id.vBorderLine);
        try {
            llRecomment.removeView(borderLine);
            llPrice.removeView(borderLine);
            llSale.removeView(borderLine);
            llHot.removeView(borderLine);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inflater.inflate(R.layout.nav_border_line_layout, res, true);
        }
    }

    public void setNavClickCallback(onNavClickCallback navClickCallback) {
        this.navClickCallback = navClickCallback;
    }

    public interface onNavClickCallback {
        void onClickRecommendSort();
        void onClickHotSort();

        void onClickPriceSortAsc();
        void onClickPriceSortDesc();

        void onClickSaleSort();

        void showFragment();

    }
}
